<template>
	<view class="" @click.stop="">
		<view class="flex_x zhNumberBox">
			<view class="minus box flex_c" @click="minus">
				<u-icon name="minus" color="#707070 " size="10"></u-icon>
			</view>
			<view class="flex1">
				<u--input type='number' inputAlign='center' border="none" v-model="value" @input='changeNum'></u--input>
			</view>
			<view class="plus box flex_c" @click="plus">
				<u-icon name="plus" color="#707070" size="10"></u-icon>
			</view>
		</view>

		<!-- 用法
		 <zhNumberBox :num.sync='num' @getNum='getNum'></zhNumberBox>
		 -->
	</view>
</template>

<script>
	export default {
		props: {
			num: {
				typeof: Number,
				default: () => 1,
			},
			name: {
				default: () => '',
			},
			min: {
				default: () => 1,
			},
			max: {
				default: () => 9999,
			},
			step: { //步长
				typeof: Number,
				default: () => 1,
			},
		},
		components: {},
		data() {
			return {
				value: '', //数量
			}
		},
		onLoad() {

		},
		onShow() {

		},
		methods: {
			minus() { //减
				if (this.value > this.min) {
					let num = Number(this.value) - Number(this.step)
					this.$emit('update:num', num)
					this.$emit('getNum', {
						value: num,
						name: this.name
					})
				}
			},
			plus() { //加
				if (this.value < this.max) {
					let num = Number(this.value) + Number(this.step)
					this.$emit('update:num', num)
					this.$emit('getNum', {
						value: num,
						name: this.name
					})
				}
			},
			changeNum(e) {
				if (e) {
					console.log(e);
					let num = Number(e)
					num = num > this.max ? this.max : num
					num = num < this.min ? this.min : num

					this.$nextTick(() => {
						this.value = num
						this.$emit('update:num', num)
						this.$emit('getNum', {
							value: num,
							name: this.name
						})
					})
				}
			},
		},
		computed: {

		},
		watch: {
			num: {
				handler(news, old) {
					this.value = Number(news)
				},
				immediate: true,
			},
		}
	}
</script>

<style lang="scss" scoped>
	.zhNumberBox {
		width: 190upx;
		height: 46upx;
		background: #FFFFFF;
		border-radius: 8upx;
		border: 2upx solid #CCCCCC;
	}

	.box {
		width: 60upx;
		height: 46upx;
	}

	.minus {
		border-right: 2upx solid #CCCCCC;
	}

	.plus {
		border-left: 2upx solid #CCCCCC;
	}
</style>